﻿using DAL.Database.Db.EF;
using Data.Entity.Basis;
using Infra.Tools.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Database.EasierMVC.DB.EF
{
    public class CustomRepository<T> : Repository<T>, IRepository<T>
        where T : class, new()
    {
        public CustomRepository(DbContext db)
            : base(db)
        { }

        public override void Delete(T entity)
        {
            if (entity is IDelete)
                (entity as IDelete).IsDeleted = true;
            else
                this.DbSet.Remove(entity);
        }

        public override void Delete(Expression<Func<T, bool>> predicate)
        {
            var result = this.Filter(predicate);

            foreach (var item in result)
            {
                if (item is IDelete)
                    (item as IDelete).IsDeleted = true;
                else
                    this.DbSet.Remove(item);
            }
        }

        public override void Restore(T entity)
        {
            if (entity is IDelete)
                (entity as IDelete).IsDeleted = false;
        }
    }
}
